#pragma once
#include<iostream>
using namespace std;
#include<sys/epoll.h>
#include <unistd.h>


class Epoll
{
    const static int g_size=256;
    const static int g_timeout=5000;
public:
    Epoll(int timeout=g_timeout):_timeout(g_timeout)
    {}
    void Create()
    {
        _epfd=epoll_create(g_size);
        if(_epfd<0)
            exit(5);
    }
    bool Del(int sock)
    {
        int n=epoll_ctl(_epfd,EPOLL_CTL_DEL,sock,nullptr);
        return n==0;
    }
    bool Ctrl(int sock,uint32_t events)
    {
        events|=EPOLLIN;
        struct epoll_event ev;
        ev.events=events;
        ev.data.fd=sock;
        int n=epoll_ctl(_epfd,EPOLL_CTL_MOD,sock,&ev);
        return n==0;
    }
    bool Add(int sock,uint32_t events)
    {
        struct epoll_event ev;
        ev.events=events;
        ev.data.fd=sock;
        int n=epoll_ctl(_epfd,EPOLL_CTL_ADD,sock,&ev);
        return n==0;
    }
    int Wait(struct epoll_event revs[],int num)
    {
        return epoll_wait(_epfd,revs,num,_timeout);
    }
    void Close()
    {
        if(_epfd>0)
            close(_epfd);
    }
    ~Epoll()
    {}

public:
    int _epfd;
    int _timeout;
};